/*******************************************************************************
 * Copyright 2008 www.softao.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.softao.ming.gwt.user.client;

import com.google.gwt.user.client.ui.Widget;

/**
 * Rule against the length of the text.
 * 
 * @author Dop Sun Jul 14, 2008
 */
public class LengthRule implements ValidationRule {
	/**
	 * @param maxLength
	 * @param trimmed
	 *            <code>true</code> if the text needs to be trimmed first
	 * @return the length rule to validate the length shorter than
	 *         <code>maxLength</code>.
	 */
	public static final LengthRule shorter(int maxLength, boolean trimmed) {
		return new LengthRule(-1, maxLength, trimmed);
	}

	/**
	 * @param minLength
	 * @param trimmed
	 *            <code>true</code> if the text needs to be trimmed first
	 * @return validates the length longer than the <code>minLength</code>.
	 */
	public static final LengthRule longer(int minLength, boolean trimmed) {
		return new LengthRule(minLength, -1, trimmed);
	}

	/**
	 * @param minLength
	 * @param maxLength
	 * @param trimmed
	 *            <code>true</code> if the text needs to be trimmed first
	 * @return the rule validates the length between <code>minLength</code> and
	 *         <code>maxLength</code>.
	 */
	public static final LengthRule between(int minLength, int maxLength,
			boolean trimmed) {
		return new LengthRule(minLength, maxLength, trimmed);
	}

	private boolean mTrimmed;

	private int mMinLength;

	private int mMaxLength;

	/**
	 * Initializes a new instance of <code>LengthRule</code>.
	 */
	public LengthRule() {
	}

	/**
	 * Initializes a new instance of <code>LengthRule</code>.
	 * 
	 * @param minLength
	 * @param maxLength
	 * @param trimmed
	 *            <code>true</code> if the text will be trimmed first.
	 */
	public LengthRule(int minLength, int maxLength, boolean trimmed) {
		this.mMinLength = minLength;
		this.mMaxLength = maxLength;
		this.mTrimmed = trimmed;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.softao.ming.gwt.user.client.ValidationRule#execute(com.google.gwt
	 * .user.client.ui.Widget)
	 */
	public boolean execute(Widget widget) {
		boolean result = false;
		WidgetReaders.StringReader reader = WidgetReaders.getStringReader();
		String text = reader.read(widget);
		if (text != null) {
			if (this.isTrimmed()) {
				text = text.trim();
			}
			if (this.mMinLength > 0) {
				result = text.length() >= this.mMinLength;
			}
			if (result && this.mMaxLength > 0) {
				result = text.length() <= this.mMaxLength;
			}
		}
		return result;
	}

	/**
	 * @return the maximum length of the text
	 */
	public final int getMaximumLength() {
		return mMaxLength;
	}

	/**
	 * @return the minimum length of the text
	 */
	public final int getMinimumLength() {
		return mMinLength;
	}

	/**
	 * @return <code>true</code> if the text will be trimmed first for
	 *         validation
	 */
	public final boolean isTrimmed() {
		return mTrimmed;
	}

	/**
	 * @param maxLength
	 *            the maximum length of the text
	 */
	public final void setMaximumLength(int maxLength) {
		mMaxLength = maxLength;
	}

	/**
	 * @param minLength
	 *            the minimum length of the text
	 */
	public final void setMinLength(int minLength) {
		mMinLength = minLength;
	}

	/**
	 * @param trimmed
	 *            <code>true</code> if the text will be trimmed first for
	 *            validation
	 */
	public final void setTrimmed(boolean trimmed) {
		mTrimmed = trimmed;
	}

}
